package com.cxyd.modules.wms.controller;

import com.cxyd.common.Utils.ExcelUtils;
import com.cxyd.common.annotation.LogOperation;
import com.cxyd.common.exception.ErrorCode;
import com.cxyd.common.page.PageData;
import com.cxyd.common.utils.ProcResult;
import com.cxyd.common.utils.Result;
import com.cxyd.common.validator.AssertUtils;
import com.cxyd.common.validator.ValidatorUtils;
import com.cxyd.common.validator.group.AddGroup;
import com.cxyd.common.validator.group.DefaultGroup;
import com.cxyd.common.validator.group.UpdateGroup;
import com.cxyd.modules.security.user.SecurityUser;
import com.cxyd.modules.security.user.UserDetail;
import com.cxyd.modules.wms.dto.AllotTempDTO;
import com.cxyd.modules.wms.dto.OrderDetailDTO;
import com.cxyd.modules.wms.excel.OrderDetailExcel;
import com.cxyd.modules.wms.service.OrderDetailService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 出库明细
 *
 * @author g_Cycle gengruyi01@163.com
 * @since 1.0.0 2023-04-27
 */
@RestController
@RequestMapping("wms/order-detail")
public class OrderDetailController {
    private final OrderDetailService orderDetailService;

    public OrderDetailController(OrderDetailService orderDetailService) {
        this.orderDetailService = orderDetailService;
    }

    @GetMapping("page")
    @RequiresPermissions("wms:order-detail:page")
    public Result<PageData<OrderDetailDTO>> page(@RequestParam Map<String, Object> params) {
        PageData<OrderDetailDTO> page = orderDetailService.page(params);

        return new Result<PageData<OrderDetailDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @RequiresPermissions("wms:order-detail:info")
    public Result<OrderDetailDTO> get(@PathVariable("id") Long id) {
        OrderDetailDTO data = orderDetailService.get(id);

        return new Result<OrderDetailDTO>().ok(data);
    }

    @PostMapping
    @LogOperation("保存")
    public Result save(@RequestBody OrderDetailDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        orderDetailService.save(dto);

        return new Result();
    }

    @PostMapping("import")
    @LogOperation("导入出库单")
   // @RequiresPermissions("wms:order-detail:import")
    public Result importOrderDetailList(@RequestParam("file") MultipartFile file) throws Exception{

        return orderDetailService.procDetailImport(file);
    }

    @PutMapping
    @LogOperation("修改")
    public Result update(@RequestBody OrderDetailDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        orderDetailService.update(dto);
        return new Result();
    }

    @DeleteMapping
    @LogOperation("删除")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");
        orderDetailService.delete(ids);
        return new Result();
    }

    @PostMapping("auto-allot/{id}")
    @LogOperation("自动配货")
    @RequiresPermissions("wms:order-detail:auto-allot")
    public Result autoAllot(@PathVariable("id") String id) {
        UserDetail user = SecurityUser.getUser();
        //效验数据
        final Map<String, Object> map = new HashMap<>();
        map.put("detailId", id);
        map.put("userId", user.getId());
        ProcResult procResult = orderDetailService.procDetailAutoAllot(map);
        //处理结果
        if (!procResult.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());
    }

    @PostMapping("manual-exec/{id}")
    @LogOperation("出库执行")
    @RequiresPermissions("wms:order-detail:manual-exec")
    public Result manualExec(@PathVariable("id") String id) {
        UserDetail user = SecurityUser.getUser();
        final Map<String, Object> map = new HashMap<>();
        map.put("detailId", id);
        map.put("userId", user.getId());
        ProcResult procResult = orderDetailService.procDetailManualExec(map);
        //处理结果
        if (!procResult.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());
    }

    @PostMapping("manual-exec")
    @LogOperation("批量执行")
    public Result batchExec(@RequestBody Long[] ids) {
        //数据校验
        AssertUtils.isArrayEmpty(ids, "id");
        UserDetail user = SecurityUser.getUser();
        ProcResult procResult = orderDetailService.procDetailBatchExec(ids, user.getId());
        //处理结果
        if (!procResult.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());
    }

    @PostMapping("manual-allot")
    @LogOperation("人工选配")
    public Result manualAllot(@RequestBody List<AllotTempDTO> dtoList) {
        ProcResult procResult = orderDetailService.procDetailManualAllot(dtoList);
        //处理结果
        if (!procResult.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());
    }

    @PostMapping("refund-allot/{id}")
    @RequiresPermissions("wms:order-detail:refund-allot")
    @LogOperation("取消配货")
    public Result releaseAllot(@PathVariable("id") String id) {
        UserDetail user = SecurityUser.getUser();
        final Map<String, Object> map = new HashMap<>();
        map.put("detailId", id);
        map.put("userId", user.getId());
        ProcResult procResult = orderDetailService.procDetailRefund(map);
        //处理结果
        if (!procResult.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());
    }

    @GetMapping("export")
    public void expiryExport(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<OrderDetailDTO> list = orderDetailService.getList(params);
        ExcelUtils.exportExcelToTarget(response, null, list, OrderDetailExcel.class);
    }
}